<?
// Stäng av mermory limit
ini_set('memory_limit', '-1');

//Globals
$POSTCONF = "/etc/postfix/main.cf";
$ADMINMAILCONF = "/etc/adminmail";
$ISPCONF = "../inc/isps";

function ldapConnect() {
	$dn  = 'cn=admin,'.$GLOBALS["dc"];    // ldap rdn or dn
        $pw = $GLOBALS["pw"];  // associated password

        $conn = ldap_connect("127.0.0.1") or die("Could not connect to LDAP server.");
        ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
        if ( !ldap_bind($conn,$dn,$pw))
                die("\nldap bind error");
        return $conn;
}

function ldapClose($conn) {
        ldap_close($conn);
}


function getLdapUsers($conn) {
        $sr=ldap_search($conn, $GLOBALS["dc"], "(&(objectclass=posixAccount)(!(cn=*$))(!(uidnumber=999)))");
	ldap_sort($conn, $sr, "cn");
        $arr = ldap_get_entries($conn, $sr);
	unset( $arr['count'] );
	return $arr;
}

function getLdapUser($conn,$cn) {
        $sr=ldap_search($conn, $GLOBALS["dc"], "(&(objectclass=posixAccount)(cn=$cn))");
        $arr = ldap_get_entries($conn, $sr);
	unset( $arr['count'] );
	return $arr;
}

function getLdapGroups($conn) {
	/*$sr=ldap_search($conn, $GLOBALS["dc"], "
		(&
			(objectclass=posixGroup)
				(!(gidnumber=512))      // Domain Admins
				(!(gidnumber=513))      // users
				(!(gidnumber=514))      // Domain Guests
				(!(gidnumber=515))      // Domain Computers
				(!(gidnumber=544))      // Adminstrators
				(!(gidnumber=548))      // Account Operators
				(!(gidnumber=550))      // Print Operators
				(!(gidnumber=551))      // Backup Operators
				(!(gidnumber=552))      // Replicators
			)
		");*/
	$sr=ldap_search($conn, $GLOBALS["dc"], "(&(objectclass=posixGroup)(!(gidnumber=512))(!(gidnumber=513))(!(gidnumber=514))(!(gidnumber=515))(!(gidnumber=544))(!(gidnumber=548))(!(gidnumber=550))(!(gidnumber=551))(!(gidnumber=552)))");
	ldap_sort($conn, $sr, "cn");
	$arr = ldap_get_entries($conn, $sr);
	unset( $arr['count'] );
	return $arr;
}

function getLdapGroup($conn,$gid) {
        $sr=ldap_search($conn, $GLOBALS["dc"], "(&(objectclass=posixGroup)(gidnumber=$gid))");
        $arr = ldap_get_entries($conn, $sr);
	unset( $arr['count'] );
	return $arr;
}

function getLdapComputers($conn) {
        $sr=ldap_search($conn, $GLOBALS["dc"], "(&(objectclass=posixAccount)(cn=*$))");
        $arr = ldap_get_entries($conn, $sr);
	unset( $arr['count'] );
	return $arr;
}











function description($info) {
	echo "$info
	";
};

function error($desc) {
	$_SESSION['error'] = $desc;
}

function info($desc) {
	$_SESSION['info'] = $desc;
}

function help($desc) {
	global $SERVICES_DIR;
	global $safe_s;

	$arr = @parse_ini_file( $SERVICES_DIR.$safe_s."/template", 1 );
	return 'OnMouseOver="helpbox_start(\''.$arr['help'][$desc].'\')" OnMouseOut="helpbox_stop()"';
}

function confirm($desc) {
	global $SERVICES_DIR;
	global $safe_s;

	$arr = @parse_ini_file( $SERVICES_DIR.$safe_s."/template", 1 );
	return 'OnClick="return confirm(\''.$arr['confirm'][$desc].'\')" OnChange="return confirm(\''.$arr['confirm'][$desc].'\')"';
}




function unhtmlspecialchars( $string ) {
	$string = str_replace ( '&amp;', '&', $string );
	$string = str_replace ( '&#039;', '\'', $string );
	$string = str_replace ( '&quot;', '\"', $string );
	$string = str_replace ( '&lt;', '<', $string );
	$string = str_replace ( '&gt;', '>', $string );
	$string = str_replace ( '+', ' ', $string );
	$string = str_replace ( '%E5', 'a', $string );
	$string = str_replace ( '%E4', 'a', $string );
	$string = str_replace ( '%F6', 'o', $string );
	$string = str_replace ( '%20', ' ', $string );
	return $string;
};

/*function aao ($string) {
	$string = str_replace ( 'å', 'a', $string );
	$string = str_replace ( 'ä', 'a', $string );
	$string = str_replace ( 'ö', 'o', $string );
	$string = str_replace ( 'ü', 'u', $string );
	return $string;
}
*/
function rmdirR($dir)  //returnerar true om lyckat, false annars
{
        if (is_dir("$dir")) {
                exec("rm -r $dir");  //� katalog, ta bort recrusivt
                if (file_exists("$dir")) { //n�ot finns d� fortfarande, =fel
                        echo "FEL: Kan inte ta bort $dir\n";
                        return false;
                }
        }
        else {
                echo "FEL: Finns inte eller ej katalog.. $dir\n";
                return false;
        }
        return true;
}


function runDir($dir, $arg = "")
{
	$files=array();
	$dh  = opendir($dir);
	while (false !== ($filename = readdir($dh))) {
		if(is_file("$dir/$filename")) {
			$files[] = $filename;
		}
	}

	sort($files);
	foreach ($files as $file)
		exec("$dir/$file $arg");
}

function event($conn,$dir) {
	if ($handle = opendir('../events/'.$dir)) {
		while (false !== ($file = readdir($handle))) {
			if ($file{0} != ".") 
				$eventfiles[] = $file;
		}
		sort($eventfiles);
		for ($iterate=0; $iterate<count($eventfiles); $iterate++) {
			include('../events/'.$dir."/".$eventfiles[$iterate]);
		}
	}
	closedir($handle);
}

function allowedCharacters($str) {
        if (! preg_match('/^[a-z0-9-_]*$/i',$str)) {
                return 0;
        }
        return 1;
}

function title(){
	global $SERVICE;

        if ( $SERVICE )
                $settings = parse_ini_file($SERVICE.'/settings');
        else
                $settings['name'] = "Meny";
        echo(ucfirst(exec("/bin/uname -n")).": ". $settings['name']);
}

if(!function_exists('write_ini_file')) {
	function write_ini_file($path, $assoc_array) {
		foreach($assoc_array as $key => $item) {
			if(is_array($item)) {
				$content .= "\n[{$key}]\n";
				foreach ($item as $key2 => $item2) {
					if(is_numeric($item2) || is_bool($item2))
						$content .= "{$key2} = {$item2}\n";
					else
						$content .= "{$key2} = \"{$item2}\"\n";
					}
			} else {
				if(is_numeric($item) || is_bool($item))
					$content .= "{$key} = {$item}\n";
				else
					$content .= "{$key} = \"{$item}\"\n";
			}
		}       

		if(!$handle = fopen($path, 'w')) {
			return false;
		}
		if(!fwrite($handle, $content)) {
			return false;
		}
		fclose($handle);
		return true;
	}
}


function sendmail($sub, $msg)
{
	global $ADMINMAILCONF;

	$email = file( $ADMINMAILCONF );
	$email = trim($email[0]);
	$host = ucfirst(exec("/bin/uname -n"));

	//$header = "Content-Type: text/plain; charset=ISO-8859-1\nContent-Transfer-Encoding: 8bit\nFrom: ".utf8_decode("$host <$email>")."\n";
	$header = "From: ".utf8_decode("$host <$email>")."\n";
	$subject = utf8_decode($host.' - '.$sub);
	$message = utf8_decode($msg);

	mb_send_mail($email, $subject, $message, $header);
}

function is_valid_email_address($email) {
	$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
	$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
	$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
			'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
	$quoted_pair = '\\x5c[\\x00-\\x7f]';
	$domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
	$quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
	$domain_ref = $atom;
	$sub_domain = "($domain_ref|$domain_literal)";
	$word = "($atom|$quoted_string)";
	$domain = "$sub_domain(\\x2e$sub_domain)*";
	$local_part = "$word(\\x2e$word)*";
	$addr_spec = "$local_part\\x40$domain";
	return preg_match("!^$addr_spec$!", $email) ? 1 : 0;
}



?>
